
#include "contract.h"

#include "Indecator/LedId.h"
#include "IO.h"

#include "BSP_Config.h"

#define LED_LEVEL_OFF	1u
#define LED_LEVEL_ON	(! LED_LEVEL_OFF)

static const IO_T LEDs[LedId_Limit] = {
	[LedId_Red ] = IO_INIT_TABLE(LED_R_GP, LED_R_PIN),
	[LedId_Green ] = IO_INIT_TABLE(LED_G_GP, LED_G_PIN),
	[LedId_Amber ] = IO_INVALID_TABLE,
	[LedId_Blue ] = IO_INVALID_TABLE,
	[LedId_White ] = IO_INVALID_TABLE,
};


uint8_t BSP_Led_GetState(uint8_t id){
	REQUIRE(id < LedId_Limit);
	
	if(id < LedId_Limit && IO_IsValid(&LEDs[id])){
		return IO_Get(&LEDs[id]) == LED_LEVEL_ON ? Led_S_ON : Led_S_OFF;
	}
	return Led_S_OFF;
}

void BSP_Led_SetState(uint8_t id, uint8_t state){
	REQUIRE(id < LedId_Limit);
	if(id < LedId_Limit && IO_IsValid(&LEDs[id])){
		IO_Set(&LEDs[id], state ? LED_LEVEL_ON : LED_LEVEL_OFF);
	}
}


//设置多个LED的状态
void BSP_Led_MultSet(uint8_t ledMap, uint8_t stateMap){
	int i;
	
	for(i = 0; i < LedId_Limit; ++i){
		if(ledMap & ( 1U << i)){
			BSP_Led_SetState(i, stateMap & ( 1U << i));
		}
	}
}

//获取所有LED的状态
uint32_t BSP_Led_MultGet(void){
	uint32_t state = 0;
	int i;

	for(i = 0; i < LedId_Limit; i++){
		if(BSP_Led_GetState(i)){
			state |= 1u << i;
		}
	}
	
	return state;
}



void BSP_Led_Init(void){
#ifdef NDEBUG 
	int i;
	gpio_pin_remap_config(GPIO_SWJ_DISABLE_REMAP, ENABLE);
	for(i = 0; i < LedId_Limit; i++){
		if(!IO_IsValid(&LEDs[i])){
			continue;
		}
	    IO_Init(&LEDs[i], IO_Mode_OUT);
		IO_Set(&LEDs[i], LED_LEVEL_OFF);
    }
#endif 
}



